﻿using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using LibKetoan;

namespace Ketoan
{
	/// <summary>
	/// Summary description for frmCtgs.
	/// </summary>
	public class frmCtgs : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.Label label5;
		private System.Windows.Forms.DateTimePicker tu;
		private System.Windows.Forms.DateTimePicker den;
		private System.Windows.Forms.DataGrid dataGrid1;
		private System.Windows.Forms.Button butLuu;
		private System.Windows.Forms.Button butKetthuc;
		private System.Windows.Forms.Button butTonghop;
		private long id,idsave;
		private int i_userid;
		private string s_mmyy,sql,s_ngay,user,mact="CTGS";
		private decimal d_sotien;
		private AccessData a;
		private DataSet ds;
		private DataSet dsct;
		private DataSet dsh=new DataSet();
		private Brush disabledBackBrush;
		private Brush disabledTextBrush;
		private Brush alertBackBrush;
		private Font alertFont;
		private Brush alertTextBrush;
		private Font currentRowFont;
		private Brush currentRowBackBrush;
		private bool afterCurrentCellChanged,bNew,afterCurrentCellChanged1;
		private int checkCol=0,iLctug,iChuong,iLk,iChucnang,iNhom,checkCol1=0;
		private System.Windows.Forms.DataGrid dataGrid2;
		private System.Windows.Forms.CheckBox chkAll;
		private System.Windows.Forms.ToolTip toolTip1;
		private System.Windows.Forms.TextBox ctgs;
		private System.Windows.Forms.TextBox noidung;
		private System.Windows.Forms.DateTimePicker ngay;
		private System.Windows.Forms.Label label6;
		private System.Windows.Forms.TextBox sotien;
		private System.Windows.Forms.ComboBox cmbctgs;
		private System.Windows.Forms.TextBox sotk;
		private System.Windows.Forms.Label label7;
		private System.Windows.Forms.Button butMoi;
		private System.Windows.Forms.Button butSua;
		private System.Windows.Forms.Button butHuy;
		private System.Windows.Forms.Button butIn;
		private System.Windows.Forms.Button butBoqua;
		private System.Windows.Forms.CheckBox chkAll1;
		private System.Windows.Forms.CheckBox chksoctgs;
		private System.Windows.Forms.Button butTao;
		private System.ComponentModel.IContainer components;

		public frmCtgs(AccessData acc,string mmyy,string ngay,int nhom,int chucnang,int lctug,int chuong,int lk,int userid)
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();
			a=acc;s_mmyy=mmyy;i_userid=userid;s_ngay=ngay;
			iNhom=nhom;iChucnang=chucnang;iLctug=lctug;iChuong=chuong;iLk=lk;
			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
					if(disabledBackBrush != null)
					{
						disabledBackBrush.Dispose();
						disabledTextBrush.Dispose();
						alertBackBrush.Dispose();
						alertFont.Dispose();
						alertTextBrush.Dispose();
						currentRowFont.Dispose();
						currentRowBackBrush.Dispose();
					}
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.components = new System.ComponentModel.Container();
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(frmCtgs));
			this.label1 = new System.Windows.Forms.Label();
			this.label2 = new System.Windows.Forms.Label();
			this.label3 = new System.Windows.Forms.Label();
			this.label4 = new System.Windows.Forms.Label();
			this.label5 = new System.Windows.Forms.Label();
			this.tu = new System.Windows.Forms.DateTimePicker();
			this.den = new System.Windows.Forms.DateTimePicker();
			this.ctgs = new System.Windows.Forms.TextBox();
			this.noidung = new System.Windows.Forms.TextBox();
			this.sotien = new System.Windows.Forms.TextBox();
			this.dataGrid1 = new System.Windows.Forms.DataGrid();
			this.butLuu = new System.Windows.Forms.Button();
			this.butKetthuc = new System.Windows.Forms.Button();
			this.butTonghop = new System.Windows.Forms.Button();
			this.dataGrid2 = new System.Windows.Forms.DataGrid();
			this.chkAll = new System.Windows.Forms.CheckBox();
			this.toolTip1 = new System.Windows.Forms.ToolTip(this.components);
			this.chkAll1 = new System.Windows.Forms.CheckBox();
			this.ngay = new System.Windows.Forms.DateTimePicker();
			this.label6 = new System.Windows.Forms.Label();
			this.cmbctgs = new System.Windows.Forms.ComboBox();
			this.sotk = new System.Windows.Forms.TextBox();
			this.label7 = new System.Windows.Forms.Label();
			this.butMoi = new System.Windows.Forms.Button();
			this.butSua = new System.Windows.Forms.Button();
			this.butHuy = new System.Windows.Forms.Button();
			this.butIn = new System.Windows.Forms.Button();
			this.butBoqua = new System.Windows.Forms.Button();
			this.chksoctgs = new System.Windows.Forms.CheckBox();
			this.butTao = new System.Windows.Forms.Button();
			((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.dataGrid2)).BeginInit();
			this.SuspendLayout();
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(-16, 289);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(168, 23);
			this.label1.TabIndex = 0;
			this.label1.Text = "Chứng từ chưa ghi sổ từ tháng :";
			this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(232, 289);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(32, 23);
			this.label2.TabIndex = 22;
			this.label2.Text = "đến :";
			this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(-8, 5);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(72, 23);
			this.label3.TabIndex = 17;
			this.label3.Text = "Số CTGS :";
			this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			// 
			// label4
			// 
			this.label4.Location = new System.Drawing.Point(176, 5);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(40, 23);
			this.label4.TabIndex = 24;
			this.label4.Text = "Ngày :";
			this.label4.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			// 
			// label5
			// 
			this.label5.Anchor = (System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right);
			this.label5.Location = new System.Drawing.Point(600, 5);
			this.label5.Name = "label5";
			this.label5.Size = new System.Drawing.Size(80, 23);
			this.label5.TabIndex = 40;
			this.label5.Text = "Số tiền :";
			this.label5.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			// 
			// tu
			// 
			this.tu.CustomFormat = "dd/MM/yyyy";
			this.tu.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.tu.Format = System.Windows.Forms.DateTimePickerFormat.Custom;
			this.tu.Location = new System.Drawing.Point(152, 291);
			this.tu.Name = "tu";
			this.tu.Size = new System.Drawing.Size(80, 21);
			this.tu.TabIndex = 5;
			this.tu.Value = new System.DateTime(2005, 5, 26, 11, 8, 30, 30);
			this.tu.KeyDown += new System.Windows.Forms.KeyEventHandler(this.tu_KeyDown);
			// 
			// den
			// 
			this.den.CustomFormat = "dd/MM/yyyy";
			this.den.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.den.Format = System.Windows.Forms.DateTimePickerFormat.Custom;
			this.den.Location = new System.Drawing.Point(264, 291);
			this.den.Name = "den";
			this.den.Size = new System.Drawing.Size(80, 21);
			this.den.TabIndex = 6;
			this.den.KeyDown += new System.Windows.Forms.KeyEventHandler(this.tu_KeyDown);
			// 
			// ctgs
			// 
			this.ctgs.BackColor = System.Drawing.SystemColors.HighlightText;
			this.ctgs.Enabled = false;
			this.ctgs.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.ctgs.Location = new System.Drawing.Point(64, 6);
			this.ctgs.Name = "ctgs";
			this.ctgs.Size = new System.Drawing.Size(112, 21);
			this.ctgs.TabIndex = 2;
			this.ctgs.Text = "";
			this.ctgs.KeyDown += new System.Windows.Forms.KeyEventHandler(this.tu_KeyDown);
			// 
			// noidung
			// 
			this.noidung.Anchor = ((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right);
			this.noidung.BackColor = System.Drawing.SystemColors.HighlightText;
			this.noidung.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.noidung.Location = new System.Drawing.Point(352, 6);
			this.noidung.Name = "noidung";
			this.noidung.Size = new System.Drawing.Size(280, 21);
			this.noidung.TabIndex = 4;
			this.noidung.Text = "";
			this.noidung.KeyDown += new System.Windows.Forms.KeyEventHandler(this.tu_KeyDown);
			// 
			// sotien
			// 
			this.sotien.Anchor = (System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right);
			this.sotien.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.sotien.ForeColor = System.Drawing.Color.Red;
			this.sotien.Location = new System.Drawing.Point(680, 6);
			this.sotien.Name = "sotien";
			this.sotien.Size = new System.Drawing.Size(104, 21);
			this.sotien.TabIndex = 21;
			this.sotien.Text = "";
			this.sotien.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
			this.sotien.KeyDown += new System.Windows.Forms.KeyEventHandler(this.tu_KeyDown);
			// 
			// dataGrid1
			// 
			this.dataGrid1.Anchor = ((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right);
			this.dataGrid1.BackgroundColor = System.Drawing.SystemColors.Control;
			this.dataGrid1.BorderStyle = System.Windows.Forms.BorderStyle.None;
			this.dataGrid1.CaptionBackColor = System.Drawing.SystemColors.Control;
			this.dataGrid1.DataMember = "";
			this.dataGrid1.FlatMode = true;
			this.dataGrid1.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
			this.dataGrid1.Location = new System.Drawing.Point(8, 11);
			this.dataGrid1.Name = "dataGrid1";
			this.dataGrid1.RowHeaderWidth = 5;
			this.dataGrid1.Size = new System.Drawing.Size(776, 276);
			this.dataGrid1.TabIndex = 18;
			this.dataGrid1.Click += new System.EventHandler(this.dataGrid1_Click);
			this.dataGrid1.CurrentCellChanged += new System.EventHandler(this.dataGrid1_CurrentCellChanged);
			// 
			// butLuu
			// 
			this.butLuu.Anchor = (System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left);
			this.butLuu.Enabled = false;
			this.butLuu.Image = ((System.Drawing.Bitmap)(resources.GetObject("butLuu.Image")));
			this.butLuu.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
			this.butLuu.Location = new System.Drawing.Point(274, 509);
			this.butLuu.Name = "butLuu";
			this.butLuu.Size = new System.Drawing.Size(60, 25);
			this.butLuu.TabIndex = 8;
			this.butLuu.Text = "    &Lưu";
			this.butLuu.Click += new System.EventHandler(this.butLuu_Click);
			// 
			// butKetthuc
			// 
			this.butKetthuc.Anchor = (System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left);
			this.butKetthuc.Image = ((System.Drawing.Bitmap)(resources.GetObject("butKetthuc.Image")));
			this.butKetthuc.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
			this.butKetthuc.Location = new System.Drawing.Point(651, 509);
			this.butKetthuc.Name = "butKetthuc";
			this.butKetthuc.Size = new System.Drawing.Size(70, 25);
			this.butKetthuc.TabIndex = 14;
			this.butKetthuc.Text = "   &Kết thúc";
			this.butKetthuc.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			this.butKetthuc.Click += new System.EventHandler(this.butKetthuc_Click);
			// 
			// butTonghop
			// 
			this.butTonghop.Anchor = (System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right);
			this.butTonghop.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
			this.butTonghop.Image = ((System.Drawing.Bitmap)(resources.GetObject("butTonghop.Image")));
			this.butTonghop.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
			this.butTonghop.Location = new System.Drawing.Point(736, 290);
			this.butTonghop.Name = "butTonghop";
			this.butTonghop.Size = new System.Drawing.Size(48, 23);
			this.butTonghop.TabIndex = 7;
			this.butTonghop.Text = "&Tìm";
			this.butTonghop.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			this.butTonghop.Click += new System.EventHandler(this.butTonghop_Click);
			// 
			// dataGrid2
			// 
			this.dataGrid2.Anchor = (((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
				| System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right);
			this.dataGrid2.BackgroundColor = System.Drawing.SystemColors.Control;
			this.dataGrid2.BorderStyle = System.Windows.Forms.BorderStyle.None;
			this.dataGrid2.CaptionBackColor = System.Drawing.SystemColors.Control;
			this.dataGrid2.DataMember = "";
			this.dataGrid2.FlatMode = true;
			this.dataGrid2.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.dataGrid2.HeaderForeColor = System.Drawing.SystemColors.ControlText;
			this.dataGrid2.Location = new System.Drawing.Point(8, 296);
			this.dataGrid2.Name = "dataGrid2";
			this.dataGrid2.RowHeaderWidth = 5;
			this.dataGrid2.Size = new System.Drawing.Size(776, 208);
			this.dataGrid2.TabIndex = 20;
			this.dataGrid2.Click += new System.EventHandler(this.dataGrid2_Click);
			this.dataGrid2.CurrentCellChanged += new System.EventHandler(this.dataGrid2_CurrentCellChanged);
			// 
			// chkAll
			// 
			this.chkAll.Appearance = System.Windows.Forms.Appearance.Button;
			this.chkAll.Font = new System.Drawing.Font("Arial", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.chkAll.Location = new System.Drawing.Point(25, 30);
			this.chkAll.Name = "chkAll";
			this.chkAll.Size = new System.Drawing.Size(23, 21);
			this.chkAll.TabIndex = 19;
			this.chkAll.Text = "...";
			this.chkAll.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
			this.toolTip1.SetToolTip(this.chkAll, "Chọn cả");
			this.chkAll.CheckedChanged += new System.EventHandler(this.chkAll_CheckedChanged);
			// 
			// chkAll1
			// 
			this.chkAll1.Appearance = System.Windows.Forms.Appearance.Button;
			this.chkAll1.Font = new System.Drawing.Font("Arial", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.chkAll1.Location = new System.Drawing.Point(25, 315);
			this.chkAll1.Name = "chkAll1";
			this.chkAll1.Size = new System.Drawing.Size(23, 21);
			this.chkAll1.TabIndex = 41;
			this.chkAll1.Text = "...";
			this.chkAll1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
			this.toolTip1.SetToolTip(this.chkAll1, "Chọn cả");
			this.chkAll1.CheckedChanged += new System.EventHandler(this.chkAll1_CheckedChanged);
			// 
			// ngay
			// 
			this.ngay.CustomFormat = "dd/MM/yyyy";
			this.ngay.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.ngay.Format = System.Windows.Forms.DateTimePickerFormat.Custom;
			this.ngay.Location = new System.Drawing.Point(216, 6);
			this.ngay.Name = "ngay";
			this.ngay.Size = new System.Drawing.Size(80, 21);
			this.ngay.TabIndex = 3;
			this.ngay.Value = new System.DateTime(2005, 5, 26, 11, 8, 30, 30);
			this.ngay.KeyDown += new System.Windows.Forms.KeyEventHandler(this.tu_KeyDown);
			// 
			// label6
			// 
			this.label6.Location = new System.Drawing.Point(272, 5);
			this.label6.Name = "label6";
			this.label6.Size = new System.Drawing.Size(80, 23);
			this.label6.TabIndex = 25;
			this.label6.Text = "Nội dung :";
			this.label6.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			// 
			// cmbctgs
			// 
			this.cmbctgs.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.cmbctgs.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.cmbctgs.Location = new System.Drawing.Point(64, 6);
			this.cmbctgs.Name = "cmbctgs";
			this.cmbctgs.Size = new System.Drawing.Size(112, 21);
			this.cmbctgs.TabIndex = 1;
			this.cmbctgs.SelectedIndexChanged += new System.EventHandler(this.cmbctgs_SelectedIndexChanged);
			// 
			// sotk
			// 
			this.sotk.Anchor = ((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right);
			this.sotk.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.sotk.Location = new System.Drawing.Point(416, 291);
			this.sotk.Name = "sotk";
			this.sotk.Size = new System.Drawing.Size(320, 21);
			this.sotk.TabIndex = 15;
			this.sotk.Text = "";
			this.sotk.TextChanged += new System.EventHandler(this.sotk_TextChanged);
			// 
			// label7
			// 
			this.label7.Location = new System.Drawing.Point(336, 289);
			this.label7.Name = "label7";
			this.label7.Size = new System.Drawing.Size(80, 23);
			this.label7.TabIndex = 23;
			this.label7.Text = "Số tài khoản :";
			this.label7.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			// 
			// butMoi
			// 
			this.butMoi.Anchor = (System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left);
			this.butMoi.Image = ((System.Drawing.Bitmap)(resources.GetObject("butMoi.Image")));
			this.butMoi.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
			this.butMoi.Location = new System.Drawing.Point(148, 509);
			this.butMoi.Name = "butMoi";
			this.butMoi.Size = new System.Drawing.Size(60, 25);
			this.butMoi.TabIndex = 10;
			this.butMoi.Text = "      &Mới";
			this.butMoi.Click += new System.EventHandler(this.butMoi_Click);
			// 
			// butSua
			// 
			this.butSua.Anchor = (System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left);
			this.butSua.Image = ((System.Drawing.Bitmap)(resources.GetObject("butSua.Image")));
			this.butSua.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
			this.butSua.Location = new System.Drawing.Point(211, 509);
			this.butSua.Name = "butSua";
			this.butSua.Size = new System.Drawing.Size(60, 25);
			this.butSua.TabIndex = 11;
			this.butSua.Text = "      &Sữa";
			this.butSua.Click += new System.EventHandler(this.butSua_Click);
			// 
			// butHuy
			// 
			this.butHuy.Anchor = (System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left);
			this.butHuy.Image = ((System.Drawing.Bitmap)(resources.GetObject("butHuy.Image")));
			this.butHuy.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
			this.butHuy.Location = new System.Drawing.Point(400, 509);
			this.butHuy.Name = "butHuy";
			this.butHuy.Size = new System.Drawing.Size(60, 25);
			this.butHuy.TabIndex = 12;
			this.butHuy.Text = "    &Hủy";
			this.butHuy.Click += new System.EventHandler(this.butHuy_Click);
			// 
			// butIn
			// 
			this.butIn.Anchor = (System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left);
			this.butIn.Image = ((System.Drawing.Bitmap)(resources.GetObject("butIn.Image")));
			this.butIn.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
			this.butIn.Location = new System.Drawing.Point(588, 509);
			this.butIn.Name = "butIn";
			this.butIn.Size = new System.Drawing.Size(60, 25);
			this.butIn.TabIndex = 13;
			this.butIn.Text = "    &In";

			// 
			// butBoqua
			// 
			this.butBoqua.Anchor = (System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left);
			this.butBoqua.Enabled = false;
			this.butBoqua.Image = ((System.Drawing.Bitmap)(resources.GetObject("butBoqua.Image")));
			this.butBoqua.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
			this.butBoqua.Location = new System.Drawing.Point(337, 509);
			this.butBoqua.Name = "butBoqua";
			this.butBoqua.Size = new System.Drawing.Size(60, 25);
			this.butBoqua.TabIndex = 9;
			this.butBoqua.Text = "&Bỏ qua";
			this.butBoqua.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			this.butBoqua.Click += new System.EventHandler(this.butBoqua_Click);
			// 
			// chksoctgs
			// 
			this.chksoctgs.Anchor = (System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left);
			this.chksoctgs.Location = new System.Drawing.Point(8, 512);
			this.chksoctgs.Name = "chksoctgs";
			this.chksoctgs.Size = new System.Drawing.Size(144, 16);
			this.chksoctgs.TabIndex = 42;
			this.chksoctgs.Text = "Số CTGS tăng tự động";
			this.chksoctgs.CheckedChanged += new System.EventHandler(this.chksoctgs_CheckedChanged);
			// 
			// butTao
			// 
			this.butTao.Anchor = (System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left);
			this.butTao.Image = ((System.Drawing.Bitmap)(resources.GetObject("butTao.Image")));
			this.butTao.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
			this.butTao.Location = new System.Drawing.Point(463, 509);
			this.butTao.Name = "butTao";
			this.butTao.Size = new System.Drawing.Size(122, 25);
			this.butTao.TabIndex = 43;
			this.butTao.Text = "&Tạo CTGS tự động";
			this.butTao.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			this.butTao.Click += new System.EventHandler(this.butTao_Click);
			// 
			// frmCtgs
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(792, 573);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.butMoi,
																		  this.butTao,
																		  this.chksoctgs,
																		  this.chkAll1,
																		  this.butBoqua,
																		  this.butIn,
																		  this.butHuy,
																		  this.butSua,
																		  this.den,
																		  this.label7,
																		  this.sotk,
																		  this.cmbctgs,
																		  this.ngay,
																		  this.label6,
																		  this.chkAll,
																		  this.butTonghop,
																		  this.butKetthuc,
																		  this.butLuu,
																		  this.sotien,
																		  this.noidung,
																		  this.ctgs,
																		  this.tu,
																		  this.label5,
																		  this.label4,
																		  this.label3,
																		  this.label2,
																		  this.label1,
																		  this.dataGrid1,
																		  this.dataGrid2});
			this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
			this.KeyPreview = true;
			this.Name = "frmCtgs";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "Chứng từ ghi sổ";
			this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
			this.Load += new System.EventHandler(this.frmCtgs_Load);
			((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.dataGrid2)).EndInit();
			this.ResumeLayout(false);

		}
		#endregion

		private void frmCtgs_Load(object sender, System.EventArgs e)
		{			
			user=a.user;

			ngay.Value=new DateTime(int.Parse(s_ngay.Substring(6,4)),int.Parse(s_ngay.Substring(3,2)),int.Parse(s_ngay.Substring(0,2)),0,0,0);
			tu.Value=new DateTime(2000+int.Parse(s_mmyy.Substring(2,2)),int.Parse(s_mmyy.Substring(0,2)),1,0,0,0);
			den.Value=new DateTime(int.Parse(s_ngay.Substring(6,4)),int.Parse(s_ngay.Substring(3,2)),int.Parse(s_ngay.Substring(0,2)),0,0,0);
			
			cmbctgs.DisplayMember="CTGS";
			cmbctgs.ValueMember="ID";

			load_head();
			id=(cmbctgs.SelectedIndex!=-1)?long.Parse(cmbctgs.SelectedValue.ToString()):-1;
			load_grid();
			AddGridTableStyle();
			
			this.disabledBackBrush = new SolidBrush(Color.FromArgb(255,255,192));
			this.disabledTextBrush = new SolidBrush(Color.FromArgb(255,0,0));

			this.alertBackBrush = new SolidBrush(SystemColors.HotTrack);
			this.alertFont = new Font(this.dataGrid1.Font.Name, this.dataGrid1.Font.Size, FontStyle.Bold);
			this.alertTextBrush = new SolidBrush(Color.White);
			
			this.currentRowFont = new Font(this.dataGrid1.Font.Name, this.dataGrid1.Font.Size, FontStyle.Regular);
			this.currentRowBackBrush = new SolidBrush(Color.FromArgb(0,255, 255));		

			load_ct();
			AddGridTableStyle1();

			chksoctgs.Checked=a.Thongso("autoctgs")=="1";
		}

		private void load_head()
		{
			sql="select id,idbc,chuong,lk,ctgs,to_char(ngaygs,'dd/mm/yyyy') as ngaygs,noidung,stien,thanggs,lctug,chucnang,userid ";
			sql+=" from "+user+".a_ctgs"+s_mmyy.Substring(2)+" where thanggs='"+s_mmyy.Substring(0,2)+"'";
//			sql+=" and idbc="+iNhom+" and chuong="+iChuong+" and lk="+iLk+" and chucnang="+iChucnang;
			sql+=" order by ctgs";
			dsh=a.get_data(sql);
			cmbctgs.DataSource=dsh.Tables[0];			
		}

		private void load_ct()
		{
			dsct=new DataSet();
			sql="select b.id,b.idct,a.soctg,to_char(a.ngaygs,'dd/mm/yyyy') as ngayg,c.ten as loai,b.noidung,d.stk as tkno,e.stk as tkco,b.stien,b.ctgs ";
			sql+=" from "+user+".a_ct"+s_mmyy.Substring(2)+" a inner join "+user+".a_ctct"+s_mmyy.Substring(2)+" b on a.id=b.idct ";
			sql+= " inner join "+user+".a_bcnghang c on a.lctug=c.id ";
			sql+= " inner join "+user+".a_dmtk d on b.tknoa=d.id ";
			sql+=" inner join "+user+".a_dmtk e on b.tkcoa=e.id ";
			sql+=" where ";
			sql+=" a.ngaygs between to_date('"+tu.Text+"','dd/mm/yy') and to_date('"+den.Text+"','dd/mm/yy')";
			if (iNhom!=0) sql+=" and a.idbc="+iNhom;
			if (iChucnang!=0) sql+=" and a.chucnang="+iChucnang;
			if (iChuong!=0) sql+=" and a.chuong="+iChuong;
			if (iLk!=0) sql+=" and b.lk="+iLk;
			if (iLctug!=0) sql+=" and a.lctug="+iLctug;
			//sql+=" and (a.ctgs is null or b.ctgs=0)";
			sql+=" and  b.ctgs=0 ";
			sql+=" order by a.ngaygs,a.soctg,c.stt,b.stt";
			dsct=a.get_data(sql);
			dsct.Tables[0].Columns.Add("Chon",typeof(bool));
			foreach(DataRow r in dsct.Tables[0].Rows) r["chon"]=false;
			dataGrid2.DataSource=dsct.Tables[0];
			CurrencyManager cm = (CurrencyManager)BindingContext[dataGrid2.DataSource,dataGrid2.DataMember]; 
			DataView dv = (DataView) cm.List; 
			dv.AllowNew = false; 
		}		

		private void load_grid()
		{
			ref_head(id);
			ds=new DataSet();
			sql="select b.id,b.idct,a.soctg,to_char(a.ngayg,'dd/mm/yyyy') as ngayg,c.ten as loai,b.noidung,d.stk as tkno,e.stk as tkco,b.stien,b.ctgs ";
			sql+=" from "+user+".a_ct"+s_mmyy.Substring(2)+" a,"+user+".a_ctct"+s_mmyy.Substring(2)+" b,"+user+".a_bcnghang c,"+user+".a_dmtk d,"+user+".a_dmtk e";
			sql+=" where a.id=b.idct and a.lctug=c.id and b.tknoa=d.id and b.tkcoa=e.id ";
			sql+=" and b.ctgs="+id;
			sql+=" order by c.stt,a.soctg,a.ngayg,b.stt";
			ds=a.get_data(sql);
			ds.Tables[0].Columns.Add("Chon",typeof(bool));
			foreach(DataRow r in ds.Tables[0].Rows) r["chon"]=false;
			dataGrid1.DataSource=ds.Tables[0];
			CurrencyManager cm = (CurrencyManager)BindingContext[dataGrid1.DataSource,dataGrid1.DataMember]; 
			DataView dv = (DataView) cm.List; 
			dv.AllowNew = false; 
		}

		private void AddGridTableStyle1()
		{
			DataGridTableStyle ts =new DataGridTableStyle();
			ts.MappingName = dsct.Tables[0].TableName;
			ts.AlternatingBackColor = Color.Beige;
			ts.BackColor = Color.GhostWhite;
			ts.ForeColor = Color.MidnightBlue;
			ts.GridLineColor = Color.RoyalBlue;
			ts.HeaderBackColor = Color.MidnightBlue;
			ts.HeaderForeColor = Color.Lavender;
			ts.SelectionBackColor = Color.FromArgb(0,255,255);
			ts.SelectionForeColor = Color.PaleGreen;
			ts.RowHeaderWidth=5;
					
			FormattableBooleanColumn discontinuedCol = new FormattableBooleanColumn();
			discontinuedCol.MappingName = "chon";
			discontinuedCol.HeaderText = "Chọn";
			discontinuedCol.Width = 30;
			discontinuedCol.AllowNull = false;

			discontinuedCol.SetCellFormat += new FormatCellEventHandler(SetCellFormat1);
			discontinuedCol.BoolValueChanged += new BoolValueChangedEventHandler(BoolValueChanged1);
			ts.GridColumnStyles.Add(discontinuedCol);
			dataGrid2.TableStyles.Add(ts);

			FormattableTextBoxColumn TextCol = new FormattableTextBoxColumn();
			TextCol.MappingName = "soctg";
			TextCol.HeaderText = "Số";
			TextCol.Width =60;
			TextCol.ReadOnly=true;
			TextCol.SetCellFormat += new FormatCellEventHandler(SetCellFormat1);
			ts.GridColumnStyles.Add(TextCol);			
			dataGrid2.TableStyles.Add(ts);

			TextCol = new FormattableTextBoxColumn();
			TextCol.MappingName = "ngayg";
			TextCol.HeaderText = "Ngày";
			TextCol.Width =65;
			TextCol.ReadOnly=true;
			TextCol.SetCellFormat += new FormatCellEventHandler(SetCellFormat1);
			ts.GridColumnStyles.Add(TextCol);			
			dataGrid2.TableStyles.Add(ts);

			TextCol = new FormattableTextBoxColumn();
			TextCol.MappingName = "loai";
			TextCol.HeaderText = "Loại";
			TextCol.Width =100;
			TextCol.ReadOnly=true;
			TextCol.SetCellFormat += new FormatCellEventHandler(SetCellFormat1);
			ts.GridColumnStyles.Add(TextCol);			
			dataGrid2.TableStyles.Add(ts);

			TextCol = new FormattableTextBoxColumn();
			TextCol.MappingName = "noidung";
			TextCol.HeaderText = "Nội dung";
			TextCol.Width =dataGrid2.Width-(70+100+100+100+100+125);
			TextCol.ReadOnly=true;
			TextCol.SetCellFormat += new FormatCellEventHandler(SetCellFormat1);
			ts.GridColumnStyles.Add(TextCol);
			dataGrid2.TableStyles.Add(ts);

			TextCol = new FormattableTextBoxColumn();
			TextCol.MappingName = "tkno";
			TextCol.HeaderText = "TK Nợ";
			TextCol.Width =100;
			TextCol.ReadOnly=true;
			TextCol.SetCellFormat += new FormatCellEventHandler(SetCellFormat1);
			ts.GridColumnStyles.Add(TextCol);
			dataGrid2.TableStyles.Add(ts);

			TextCol = new FormattableTextBoxColumn();
			TextCol.MappingName = "tkco";
			TextCol.HeaderText = "TK Có";
			TextCol.Width =100;
			TextCol.ReadOnly=true;
			TextCol.SetCellFormat += new FormatCellEventHandler(SetCellFormat1);
			ts.GridColumnStyles.Add(TextCol);
			dataGrid2.TableStyles.Add(ts);

			TextCol=new FormattableTextBoxColumn();
			TextCol.MappingName = "stien";
			TextCol.HeaderText = "Số tiền";
			TextCol.Format="### ### ### ##0.00";
			TextCol.Width =100;
			TextCol.ReadOnly=true;
			TextCol.Alignment=HorizontalAlignment.Right;
			ts.GridColumnStyles.Add(TextCol);
			dataGrid2.TableStyles.Add(ts);
		}
	
		private void SetCellFormat1(object sender, DataGridFormatCellEventArgs e)
		{
			try
			{
				bool discontinued = (bool) ((e.Column != 0) ? this.dataGrid2[e.Row, 0] : e.CurrentCellValue);
				if(e.Column > 0 && (bool)(this.dataGrid2[e.Row, 0]))//discontinued)
				{
					e.ForeBrush = this.disabledTextBrush;
				}
				else if(e.Column > 0 && e.Row == this.dataGrid2.CurrentRowIndex)
				{
					e.BackBrush = this.currentRowBackBrush;
					e.TextFont = this.currentRowFont;
				}
			}
			catch{}
		}

		private void BoolValueChanged1(object sender, BoolValueChangedEventArgs e)
		{
			this.dataGrid2.EndEdit(this.dataGrid2.TableStyles[0].GridColumnStyles["Discontinued"],e.Row, false);
			RefreshRow1(e.Row);
			this.dataGrid2.BeginEdit(this.dataGrid2.TableStyles[0].GridColumnStyles["Discontinued"],e.Row);
		}
		private void RefreshRow1(int row)
		{
			Rectangle rect = this.dataGrid2.GetCellBounds(row, 0);
			rect = new Rectangle(rect.Right, rect.Top, this.dataGrid2.Width, rect.Height);
			this.dataGrid2.Invalidate(rect);
		}

		private void AddGridTableStyle()
		{
			DataGridTableStyle ts =new DataGridTableStyle();
			ts.MappingName = ds.Tables[0].TableName;
			ts.AlternatingBackColor = Color.Beige;
			ts.BackColor = Color.GhostWhite;
			ts.ForeColor = Color.MidnightBlue;
			ts.GridLineColor = Color.RoyalBlue;
			ts.HeaderBackColor = Color.MidnightBlue;
			ts.HeaderForeColor = Color.Lavender;
			ts.SelectionBackColor = Color.FromArgb(0,255,255);
			ts.SelectionForeColor = Color.PaleGreen;
			ts.RowHeaderWidth=5;
					
			FormattableBooleanColumn discontinuedCol = new FormattableBooleanColumn();
			discontinuedCol.MappingName = "chon";
			discontinuedCol.HeaderText = "Chọn";
			discontinuedCol.Width = 30;
			discontinuedCol.AllowNull = false;

			discontinuedCol.SetCellFormat += new FormatCellEventHandler(SetCellFormat);
			discontinuedCol.BoolValueChanged += new BoolValueChangedEventHandler(BoolValueChanged);
			ts.GridColumnStyles.Add(discontinuedCol);
			dataGrid1.TableStyles.Add(ts);

			FormattableTextBoxColumn TextCol = new FormattableTextBoxColumn();
			TextCol.MappingName = "soctg";
			TextCol.HeaderText = "Số";
			TextCol.Width =60;
			TextCol.ReadOnly=true;
			TextCol.SetCellFormat += new FormatCellEventHandler(SetCellFormat);
			ts.GridColumnStyles.Add(TextCol);			
			dataGrid1.TableStyles.Add(ts);

			TextCol = new FormattableTextBoxColumn();
			TextCol.MappingName = "ngayg";
			TextCol.HeaderText = "Ngày";
			TextCol.Width =65;
			TextCol.ReadOnly=true;
			TextCol.SetCellFormat += new FormatCellEventHandler(SetCellFormat);
			ts.GridColumnStyles.Add(TextCol);			
			dataGrid1.TableStyles.Add(ts);

			TextCol = new FormattableTextBoxColumn();
			TextCol.MappingName = "loai";
			TextCol.HeaderText = "Loại";
			TextCol.Width =100;
			TextCol.ReadOnly=true;
			TextCol.SetCellFormat += new FormatCellEventHandler(SetCellFormat);
			ts.GridColumnStyles.Add(TextCol);			
			dataGrid1.TableStyles.Add(ts);

			TextCol = new FormattableTextBoxColumn();
			TextCol.MappingName = "noidung";
			TextCol.HeaderText = "Nội dung";
			TextCol.Width =dataGrid1.Width-(70+100+100+100+100+125);
			TextCol.ReadOnly=true;
			TextCol.SetCellFormat += new FormatCellEventHandler(SetCellFormat);
			ts.GridColumnStyles.Add(TextCol);
			dataGrid1.TableStyles.Add(ts);

			TextCol = new FormattableTextBoxColumn();
			TextCol.MappingName = "tkno";
			TextCol.HeaderText = "TK Nợ";
			TextCol.Width =100;
			TextCol.ReadOnly=true;
			TextCol.SetCellFormat += new FormatCellEventHandler(SetCellFormat);
			ts.GridColumnStyles.Add(TextCol);
			dataGrid1.TableStyles.Add(ts);

			TextCol = new FormattableTextBoxColumn();
			TextCol.MappingName = "tkco";
			TextCol.HeaderText = "TK Có";
			TextCol.Width =100;
			TextCol.ReadOnly=true;
			TextCol.SetCellFormat += new FormatCellEventHandler(SetCellFormat);
			ts.GridColumnStyles.Add(TextCol);
			dataGrid1.TableStyles.Add(ts);

			TextCol=new FormattableTextBoxColumn();
			TextCol.MappingName = "stien";
			TextCol.HeaderText = "Số tiền";
			TextCol.Format="### ### ### ##0.00";
			TextCol.Width =100;
			TextCol.ReadOnly=true;
			TextCol.Alignment=HorizontalAlignment.Right;
			ts.GridColumnStyles.Add(TextCol);
			dataGrid1.TableStyles.Add(ts);
		}
	
		private void SetCellFormat(object sender, DataGridFormatCellEventArgs e)
		{
			try
			{
				bool discontinued = (bool) ((e.Column != 0) ? this.dataGrid1[e.Row, 0] : e.CurrentCellValue);
				if(e.Column > 0 && (bool)(this.dataGrid1[e.Row, 0]))//discontinued)
				{
					e.ForeBrush = this.disabledTextBrush;
				}
				else if(e.Column > 0 && e.Row == this.dataGrid1.CurrentRowIndex)
				{
					e.BackBrush = this.currentRowBackBrush;
					e.TextFont = this.currentRowFont;
				}
			}
			catch{}
		}

		private void BoolValueChanged(object sender, BoolValueChangedEventArgs e)
		{
			this.dataGrid1.EndEdit(this.dataGrid1.TableStyles[0].GridColumnStyles["Discontinued"],e.Row, false);
			RefreshRow(e.Row);
			this.dataGrid1.BeginEdit(this.dataGrid1.TableStyles[0].GridColumnStyles["Discontinued"],e.Row);
		}
		private void RefreshRow(int row)
		{
			Rectangle rect = this.dataGrid1.GetCellBounds(row, 0);
			rect = new Rectangle(rect.Right, rect.Top, this.dataGrid1.Width, rect.Height);
			this.dataGrid1.Invalidate(rect);
		}

		private void dataGrid1_CurrentCellChanged(object sender, System.EventArgs e)
		{
			if((bool)this.dataGrid1[this.dataGrid1.CurrentRowIndex, checkCol])
				this.dataGrid1.CurrentCell = new DataGridCell(this.dataGrid1.CurrentRowIndex, checkCol);
			afterCurrentCellChanged = true;
		}

		private void dataGrid1_Click(object sender, System.EventArgs e)
		{
			Point pt = this.dataGrid1.PointToClient(Control.MousePosition);
			DataGrid.HitTestInfo hti = this.dataGrid1.HitTest(pt);
			BindingManagerBase bmb = this.BindingContext[this.dataGrid1.DataSource, this.dataGrid1.DataMember];
			if(afterCurrentCellChanged && hti.Row < bmb.Count 
				&& hti.Type == DataGrid.HitTestType.Cell 
				&&  hti.Column == checkCol )
			{	
				this.dataGrid1[hti.Row, checkCol] = !(bool)this.dataGrid1[hti.Row, checkCol];
				RefreshRow(hti.Row);
			}
			afterCurrentCellChanged = false;
		}

		
		private void dataGrid2_CurrentCellChanged(object sender, System.EventArgs e)
		{
			if((bool)this.dataGrid2[this.dataGrid2.CurrentRowIndex, checkCol1])
				this.dataGrid2.CurrentCell = new DataGridCell(this.dataGrid2.CurrentRowIndex, checkCol1);
			afterCurrentCellChanged1 = true;
		}

		private void dataGrid2_Click(object sender, System.EventArgs e)
		{
			Point pt = this.dataGrid2.PointToClient(Control.MousePosition);
			DataGrid.HitTestInfo hti = this.dataGrid2.HitTest(pt);
			BindingManagerBase bmb = this.BindingContext[this.dataGrid2.DataSource, this.dataGrid2.DataMember];
			if(afterCurrentCellChanged1 && hti.Row < bmb.Count 
				&& hti.Type == DataGrid.HitTestType.Cell 
				&&  hti.Column == checkCol1 )
			{	
				this.dataGrid2[hti.Row, checkCol1] = !(bool)this.dataGrid2[hti.Row, checkCol1];
				RefreshRow1(hti.Row);
			}
			afterCurrentCellChanged1 = false;
		}

		private void tu_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if (e.KeyCode==Keys.Enter) SendKeys.Send("{Tab}");
		}

		private void ref_grid()
		{
			CurrencyManager cm= (CurrencyManager)BindingContext[dataGrid2.DataSource];
			DataView dv=(DataView)cm.List;
			sql="";
			if (sotk.Text!="")
			{
				if (sql!="") sql+=" and ";
				sql+=" (tkno like '%"+sotk.Text+"%' or tkco like '%"+sotk.Text+"%')";
			}
			dv.RowFilter=sql;
		}

		private void butTonghop_Click(object sender, System.EventArgs e)
		{
			load_ct();
		}
		
		private bool kiemtra()
		{
			if (ctgs.Text=="" && ctgs.Enabled)
			{
				MessageBox.Show("Nhập chứng từ ghi sổ !",a.Msg);
				if (ctgs.Enabled) ctgs.Focus();
				return false;
			}
			if (noidung.Text=="")
			{
				MessageBox.Show("Nhập nội dung chứng từ ghi sổ !",a.Msg);
				noidung.Focus();
				return false;
			}
			if (ds.Tables[0].Select("chon=false").Length==0 && dsct.Tables[0].Select("chon=true").Length==0)
			{
				MessageBox.Show("Chọn chứng từ cần ghi sổ !",a.Msg);
				dataGrid2.Focus();
				return false;
			}
			if (a.get_data("select * from "+user+".a_ctgs"+s_mmyy.Substring(2)+" where id<>"+id+" and ctgs='"+ctgs.Text+"' and thanggs='"+s_mmyy.Substring(0,2)+"'").Tables[0].Rows.Count>0)
			{
				MessageBox.Show("Số chứng từ "+ctgs.Text+" đã nhập !",a.Msg);
				if (ctgs.Enabled) ctgs.Focus();
				return false;
			}
			return true;
		}

		private void butLuu_Click(object sender, System.EventArgs e)
		{
			if (!kiemtra()) return;
			CurrencyManager cm= (CurrencyManager)BindingContext[dataGrid2.DataSource];
			DataView dv=(DataView)cm.List;
			dv.RowFilter="";
			id=(bNew)?a.getIdctgs():id;			
			long idct=0;
			foreach(DataRow r in dsct.Tables[0].Select("true","id"))
			{
				if (r["chon"].ToString().Trim().ToLower()=="true")
				{
					if (idct!=long.Parse(r["idct"].ToString()))
					{
						idct=long.Parse(r["idct"].ToString());
						a.execute_data("update "+user+".a_ct"+s_mmyy.Substring(2)+" set ctgs='"+ctgs.Text+"' where id="+idct);
					}
					a.execute_data("update "+user+".a_ctct"+s_mmyy.Substring(2)+" set ctgs="+id+" where id="+long.Parse(r["id"].ToString()));
				}
			}
			idct=0;
			foreach(DataRow r in ds.Tables[0].Select("true","id"))
			{
				if (r["chon"].ToString().Trim().ToLower()=="true")
				{
					a.execute_data("update "+user+".a_ctct"+s_mmyy.Substring(2)+" set ctgs=0 where id="+long.Parse(r["id"].ToString()));
					if (idct!=long.Parse(r["idct"].ToString()))
					{
						if (idct!=0)
						{
							if (a.get_data("select * from "+user+".a_ctct"+s_mmyy.Substring(2)+" where idct="+idct+" and ctgs<>0").Tables[0].Rows.Count==0)
								a.execute_data("update "+user+".a_ct"+s_mmyy.Substring(2)+" set ctgs='' where id="+idct);
						}
						idct=long.Parse(r["idct"].ToString());
					}
				}
			}
			if (idct!=0)
			{
				if (a.get_data("select * from "+user+".a_ctct"+s_mmyy.Substring(2)+" where idct="+idct+" and ctgs<>0").Tables[0].Rows.Count==0)
					a.execute_data("update "+user+".a_ct"+s_mmyy.Substring(2)+" set ctgs='' where id="+idct);
			}
			d_sotien=0;
			sql="select sum(b.stien) as stien ";
			sql+=" from "+user+".a_ct"+s_mmyy.Substring(2)+" a,"+user+".a_ctct"+s_mmyy.Substring(2)+" b,"+user+".a_bcnghang c,"+user+".a_dmtk d,"+user+".a_dmtk e";
			sql+=" where a.id=b.idct and a.lctug=c.id and b.tknoa=d.id and b.tkcoa=e.id ";
			sql+=" and b.ctgs="+id;
			foreach(DataRow r in a.get_data(sql).Tables[0].Rows) d_sotien+=(r["stien"].ToString()!="")?decimal.Parse(r["stien"].ToString()):0;
			sotien.Text=d_sotien.ToString("###,###,###,###.##");
			sotien.Update();
			a.updCtgs(s_mmyy,id,iNhom,iChuong,iLk,ctgs.Text,ngay.Text,noidung.Text,d_sotien,iLctug,iChucnang,i_userid);
			ena_object(false);
			load_head();
			cmbctgs.SelectedValue=id;
			load_grid();		
			load_ct();
			butMoi.Focus();
		}

		private void butKetthuc_Click(object sender, System.EventArgs e)
		{
			a.close();System.GC.Collect();this.Close();
		}

		private void cmbctgs_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			if (this.ActiveControl==cmbctgs && cmbctgs.SelectedIndex!=-1)
			{
				id=long.Parse(cmbctgs.SelectedValue.ToString());				
				load_grid();
			}
		}

		private void ref_head(long id)
		{
			DataRow r=a.getrowbyid(dsh.Tables[0],"id="+id);
			if (r!=null)
			{
				ctgs.Text=r["ctgs"].ToString();
				noidung.Text=r["noidung"].ToString();
				d_sotien=decimal.Parse(r["stien"].ToString());
				sotien.Text=d_sotien.ToString("###,###,###,###.##");
				string _ngay=r["ngaygs"].ToString();
				ngay.Value=new DateTime(int.Parse(_ngay.Substring(6,4)),int.Parse(_ngay.Substring(3,2)),int.Parse(_ngay.Substring(0,2)),0,0,0);
			}
		}
		private void sotk_TextChanged(object sender, System.EventArgs e)
		{
			if (this.ActiveControl==sotk) ref_grid();
		}

		private void ena_object(bool ena)
		{
			cmbctgs.Visible=!ena;
			if (!chksoctgs.Checked) ctgs.Enabled=ena;
			ngay.Enabled=noidung.Enabled=ena;
			butTao.Enabled=butMoi.Enabled=butSua.Enabled=butLuu.Enabled=butHuy.Enabled=butIn.Enabled=butKetthuc.Enabled=!ena;
			butLuu.Enabled=butBoqua.Enabled=ena;
		}

		private void butMoi_Click(object sender, System.EventArgs e)
		{
			if (a.bKhoaso(s_mmyy))
			{
				MessageBox.Show("Số liệu tháng "+s_mmyy.Substring(0,2)+" năm 20"+s_mmyy.Substring(2)+" đã khóa !",a.Msg);
				return;
			}
			bNew=true;
			id=0;
			idsave=(cmbctgs.SelectedIndex!=-1)?long.Parse(cmbctgs.SelectedValue.ToString()):-1;
			ctgs.Text=noidung.Text=sotien.Text="";
			if (chksoctgs.Checked) 
//				ctgs.Text=mact+a.get_ctgs(s_mmyy,"ctgs","idbc="+iNhom+" and chuong="+iChuong+" and chucnang="+iChucnang+" and lk="+iLk+" and thanggs='"+s_mmyy.Substring(0,2)+"' and lctug="+iLctug,5,4)+"/"+s_mmyy.Substring(0,2);
				ctgs.Text=mact+a.get_ctgs(s_mmyy,"ctgs"," thanggs='"+s_mmyy.Substring(0,2)+"'",5,4)+"/"+s_mmyy.Substring(0,2);
			ds.Clear();
			ena_object(true);
			if (ctgs.Enabled) ctgs.Focus();
			else ngay.Focus();
		}

		private void butSua_Click(object sender, System.EventArgs e)
		{
			if (cmbctgs.SelectedIndex==-1) return;
			if (a.bKhoaso(s_mmyy))
			{
				MessageBox.Show("Số liệu tháng "+s_mmyy.Substring(0,2)+" năm 20"+s_mmyy.Substring(2)+" đã khóa !",a.Msg);
				return;
			}
			bNew=false;
			id=idsave=(cmbctgs.SelectedIndex!=-1)?long.Parse(cmbctgs.SelectedValue.ToString()):-1;
			ena_object(true);
		}

		private void butBoqua_Click(object sender, System.EventArgs e)
		{
			cmbctgs.SelectedValue=id=idsave;
			load_grid();
			ena_object(false);
		}

		private void butHuy_Click(object sender, System.EventArgs e)
		{
			if (cmbctgs.SelectedIndex==-1) return;
			if (a.bKhoaso(s_mmyy))
			{
				MessageBox.Show("Số liệu tháng "+s_mmyy.Substring(0,2)+" năm 20"+s_mmyy.Substring(2)+" đã khóa !",a.Msg);
				return;
			}
			if (MessageBox.Show("Đồng ý hủy chứng từ ghi sổ "+cmbctgs.Text+" ?",a.Msg,MessageBoxButtons.YesNo,MessageBoxIcon.Question)==DialogResult.Yes)
			{
				id=long.Parse(cmbctgs.SelectedValue.ToString());
				int itable = a.tableid("", "a_ctgs"+s_mmyy.Substring(2));
				a.upd_eve_tables(itable, i_userid, "del");
				a.upd_eve_upd_del(itable, i_userid, "del", a.fields("a_ctgs"+s_mmyy.Substring(2), "id=" +id));
			   /*
				foreach(DataRow r in a.get_data("select distinct a.id from "+user+".a_ct"+s_mmyy.Substring(2)+" a,"+user+".a_ctct"+s_mmyy.Substring(2)+" b where a.id=b.idct and b.ctgs="+id).Tables[0].Rows)
					a.execute_data("update "+user+".a_ct"+s_mmyy.Substring(2)+" set ctgs='' where id="+long.Parse(r["id"].ToString()));
				*/
				a.execute_data("update "+user+".a_ctct"+s_mmyy.Substring(2)+" set ctgs=0 where ctgs="+id);
				a.execute_data("delete from "+user+".a_ctgs"+s_mmyy.Substring(2)+" where id="+id);
			}
			load_head();
			id=(cmbctgs.SelectedIndex!=-1)?long.Parse(cmbctgs.SelectedValue.ToString()):-1;
			load_grid();
			load_ct();
		}

		private void chkAll_CheckedChanged(object sender, System.EventArgs e)
		{
			if (this.ActiveControl==chkAll)
				foreach(DataRow r in ds.Tables[0].Rows) r["chon"]=(chkAll.Checked)?true:false;
		}

		private void chkAll1_CheckedChanged(object sender, System.EventArgs e)
		{
			if (this.ActiveControl==chkAll1)
				foreach(DataRow r in dsct.Tables[0].Rows) r["chon"]=(chkAll1.Checked)?true:false;
		}

		private void chksoctgs_CheckedChanged(object sender, System.EventArgs e)
		{
			if (this.ActiveControl==chksoctgs)
			{
				a.writeXml("a_thongso","autoctgs",(chksoctgs.Checked)?"1":"0");
				ctgs.Enabled=!chksoctgs.Checked;
			}
		}

		private void butTao_Click(object sender, System.EventArgs e)
		{
			sql="select distinct a.ctgs from "+user+".a_ct"+s_mmyy.Substring(2)+" a,"+user+".a_ctct"+s_mmyy.Substring(2)+" b,"+user+".a_bcnghang c,"+user+".a_dmtk d,"+user+".a_dmtk e";
			sql+=" where a.id=b.idct and a.lctug=c.id and b.tknoa=d.id and b.tkcoa=e.id ";
			sql+=" and a.ngaygs between to_date('"+tu.Text+"','dd/mm/yy') and to_date('"+den.Text+"','dd/mm/yy')";
			if (iNhom!=0) sql+=" and a.idbc="+iNhom;
			if (iChucnang!=0) sql+=" and a.chucnang="+iChucnang;
			if (iChuong!=0) sql+=" and a.chuong="+iChuong;
			if (iLk!=0) sql+=" and b.lk="+iLk;
			if (iLctug!=0) sql+=" and a.lctug="+iLctug;
			sql+=" and (a.ctgs is null or b.ctgs=0)";
		

			DataTable tmp=a.get_data(sql).Tables[0];
			if (tmp.Rows.Count==0) MessageBox.Show("Không có số liệu !",a.Msg);
			else
			{
				foreach(DataRow r1 in tmp.Select("ctgs is not null"))
				{
					id=a.getIdctgs();
					d_sotien=0;
					/*sql="select b.id,b.idct,a.soctg,to_char(a.ngayg,'dd/mm/yyyy') as ngayg,c.ten as loai,b.noidung,d.stk as tkno,e.stk as tkco,b.stien,b.ctgs ";
					sql+=" from "+user+".a_ct"+s_mmyy.Substring(2)+" a,"+user+".a_ctct"+s_mmyy.Substring(2)+" b,"+user+".a_bcnghang c,"+user+".a_dmtk d,"+user+".a_dmtk e";
					sql+=" where a.id=b.idct and a.lctug=c.id and b.tknoa=d.id and b.tkcoa=e.id ";
					sql+=" and a.ngaygs between to_date('"+tu.Text+"','dd/mm/yy') and to_date('"+den.Text+"','dd/mm/yy')";
					if (iNhom!=0) sql+=" and a.idbc="+iNhom;
					if (iChucnang!=0) sql+=" and a.chucnang="+iChucnang;
					if (iChuong!=0) sql+=" and a.chuong="+iChuong;
					if (iLk!=0) sql+=" and b.lk="+iLk;
					if (iLctug!=0) sql+=" and a.lctug="+iLctug;
					sql+=" and a.ctgs='"+r1["ctgs"].ToString()+"'";
					sql+=" order by c.stt,a.soctg,a.ngayg,b.stt";*/

					sql="select b.id,b.idct,a.soctg,to_char(a.ngayg,'dd/mm/yyyy') as ngayg,c.ten as loai,b.noidung,d.stk as tkno,e.stk as tkco,b.stien,b.ctgs ";
					sql+=" from "+user+".a_ct"+s_mmyy.Substring(2)+" a inner join "+user+".a_ctct"+s_mmyy.Substring(2)+" b on a.id=b.idct ";
					sql+= " inner join "+user+".a_bcnghang c on a.lctug=c.id ";
					sql+= " inner join "+user+".a_dmtk d on b.tknoa=d.id ";
					sql+=" inner join "+user+".a_dmtk e on b.tkcoa=e.id ";
					sql+=" where ";
					sql+=" a.ngaygs between to_date('"+tu.Text+"','dd/mm/yy') and to_date('"+den.Text+"','dd/mm/yy')";
					if (iNhom!=0) sql+=" and a.idbc="+iNhom;
					if (iChucnang!=0) sql+=" and a.chucnang="+iChucnang;
					if (iChuong!=0) sql+=" and a.chuong="+iChuong;
					if (iLk!=0) sql+=" and b.lk="+iLk;
					if (iLctug!=0) sql+=" and a.lctug="+iLctug;
					//sql+=" and (a.ctgs is null or b.ctgs=0)";
					sql+=" and b.ctgs=0 and a.ctgs='"+r1["ctgs"].ToString()+"'";
					sql+=" order by c.stt,a.soctg,a.ngayg,b.stt";

					foreach(DataRow r in a.get_data(sql).Tables[0].Rows)
					{
						d_sotien+=decimal.Parse(r["stien"].ToString());
						a.execute_data("update "+user+".a_ctct"+s_mmyy.Substring(2)+" set ctgs="+id+" where id="+long.Parse(r["id"].ToString()));
						//a.execute_data("update "+user+".a_ct"+s_mmyy.Substring(2)+" set ctgs='"+ctgs.Text+"' where id="+idct);
					}
					a.updCtgs(s_mmyy,id,iNhom,iChuong,iLk,r1["ctgs"].ToString(),ngay.Text,noidung.Text,d_sotien,iLctug,iChucnang,i_userid);
				}
				MessageBox.Show("Đã tạo chứng từ ghi sổ !",a.Msg);
				load_head();
				id=(cmbctgs.SelectedIndex!=-1)?long.Parse(cmbctgs.SelectedValue.ToString()):1;
				load_grid();
				load_ct();
			}
		}
		
	}
}